import os
from types import SimpleNamespace

import pygame

from common import black, blue, font_path, go_next_level, green, pink, white

# 设置窗口
screen_width, screen_height = 1200, 500

# 加载图片
images = []
image_directory = "./assets/game_6/"  # 图片文件夹路径
for i in range(25):
    image_path = os.path.join(image_directory, f"{i+1}.png")
    image = pygame.image.load(image_path)
    image = pygame.transform.scale(image, (1200, 250))
    images.append(image)

current_image = 0

button_text_font = pygame.font.Font(font_path, 40)

button_start = SimpleNamespace(
    label="start",
    rect=pygame.Rect(100, 100, 150, 50),
    color=pink,
    text="开始",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_stop = SimpleNamespace(
    label="stop",
    rect=pygame.Rect(300, 100, 150, 50),
    color=green,
    text="结束",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_next_level = SimpleNamespace(
    label="next_level",
    rect=pygame.Rect(500, 100, 150, 50),
    color=blue,
    text="下一关",
    font_color=black,
    font=button_text_font,
    selected=-1,
)


# 控制变量
running = True
show_image = False
clock = pygame.time.Clock()


def draw_button(screen, button):
    text = button_text_font.render(button.text, True, button.font_color)
    text_rect = text.get_rect(center=button.rect.center)
    # 绘制按钮
    pygame.draw.rect(screen, button.color, button.rect)
    # 绘制文字
    screen.blit(text, text_rect)


# 游戏主循环
def draw(screen):
    global running, show_image, current_image, last_update

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if button_start.rect.collidepoint(event.pos):
                show_image = True
                last_update = pygame.time.get_ticks()
                running = True
            if button_stop.rect.collidepoint(event.pos):
                running = False
            if button_next_level.rect.collidepoint(event.pos):
                go_next_level()

    screen.fill(white)

    # 绘制按钮
    draw_button(screen, button_start)
    draw_button(screen, button_stop)
    draw_button(screen, button_next_level)

    # 显示图片
    if show_image:
        current_time = pygame.time.get_ticks()
        if current_time - last_update > 1000 and running:
            last_update = current_time
            current_image = (current_image + 1) % len(images)
        screen.blit(images[current_image], (0, 250))

    pygame.display.flip()
    clock.tick(30)
